package com.ssuu.ssuusnow.project.manage.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssuu.ssuusnow.project.manage.role.entity.Role;
import com.ssuu.ssuusnow.project.manage.role.mapper.RoleMapper;
import com.ssuu.ssuusnow.project.manage.role.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssuu.ssuusnow.project.manage.user.entity.User;
import com.ssuu.ssuusnow.utils.security.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author sun
 * @since 2023-06-14
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    RoleMapper roleMapper;

    @Override
    public Object listTo(Role t) {
        QueryWrapper<Role> wrappers = new QueryWrapper<>();
        wrappers.orderByDesc("sort");
        wrappers.and(wrapper -> {
            wrapper.eq("isdel", false);
            wrapper.like("rolename", Objects.isNull(t.getRolename()) ? "" : t.getRolename());
            if (!Objects.isNull(t.getEnterpriseId())) {
                wrapper.eq("enterpriseId", t.getEnterpriseId());
            }
//        wrapper.eq("status", Objects.isNull(t.getStatus())?"":t.getStatus());
        });
        Page<Role> page = new Page<>(t.getCurrentPage(),t.getPageSize());
        Object userPage = t.getPageSize() > 0 && t.getCurrentPage() > 0 ? roleMapper.selectPage(page, wrappers) : roleMapper.selectList(wrappers);
        return userPage;
    }

    @Override
    public int AddTo(Role t) {
        t.setCreateTime(LocalDateTime.now());
        t.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        t.setUpdateby(lou.getId());
        t.setEnterpriseId(lou.getEnterpriseId());
        return roleMapper.insert(t);
    }

    @Override
    public int UpdateTo(Role t) {
        t.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        t.setUpdateby(lou.getId());
        return roleMapper.updateById(t);
    }

    @Override
    public int DelTo(Long t) {
        Role r = new Role();
        r.setId(t);
        r.setIsdel(true);
        r.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        r.setUpdateby(lou.getId());
        return roleMapper.updateById(r);
    }
}
